package app.auto.runner.base.utility;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;

import app.auto.AndroidInstance;
import app.auto.runner.base.intf.FunCallback;
import app.auto.runner.base.intf.ListBuilder;
import app.auto.runner.base.intf.MapBuilder;
import app.auto.runner.base.intf.json.JsonToMapUtils;
import app.auto.runner.base.intf.json.JsonUtil;

/**
 * Created by Administrator on 2017/11/18.
 */
public class MapUtil extends AndroidInstance {

    public static List richMap(String name, String value, Map params, String[] strings, FunCallback funCallback) {
        return richMapPrivate(name, value, params, strings, funCallback);
    }

    private static List richMapPrivate(String name, String value, Map params, String[] strings, FunCallback funCallback) {
        ListBuilder listBuilder = ListBuilder.build();
        int i = 1;
        for (Object key : params.keySet()) {
            MapBuilder mapBuilder = MapBuilder.build();
            mapBuilder.add(name, key.toString());
            mapBuilder.add(value, params.get(key));
            if (strings != null) {
                for (String s : strings) {
                    mapBuilder.add(s, "field" + i);
                }
                i++;
            }
            if (funCallback != null) {
                funCallback.onCallback(key.toString(), mapBuilder);
            }
            listBuilder.add(mapBuilder.get());
        }
        return listBuilder.get();
    }

    private List<Map> list;


    public static MapUtil build() {
        return new MapUtil();
    }

    public static MapUtil with(Map m) {
        return new MapUtil().setMap(m);
    }

    Map m;

    public MapUtil setMap(Map map) {
        this.m = map;
        return this;
    }

    public MapUtil copyValue(String actyDetail, String actybody, Map itemAtPosition) {
        itemAtPosition.put(actyDetail, itemAtPosition.get(actybody));
        return this;
    }

    public Map get() {
        return m;
    }

    public MapUtil get(List<Map> list) {
        this.list = list;
        return this;
    }

    public MapUtil get(String json) {
        list = (List<Map>) JsonUtil.extractJsonRightValue(json);
        return this;
    }

    public MapUtil sure(String key_name_value) {
        String[] nv = key_name_value.split("-");
        return sure(nv[0], nv[1]);
    }

    public MapUtil sure(String key_name, String key_value) {
        this.key_name = key_name;
        this.key_value = key_value;
        return this;
    }

    private String key_name;
    private String key_value;


    private TreeMap<String, Object> map = new TreeMap<String, Object>();

    public MapUtil transfer() {
        for (int i = 0; i < list.size(); i++) {
            String name = list.get(i).get(key_name).toString();

            String value = list.get(i).get(key_value).toString();
            map.put(name, value);
        }
        return this;
    }

    public Map toMap() {
        return map;
    }

    public String toJson() {
        return JsonToMapUtils.entityToJson(map);
    }

    @Override
    public AndroidInstance use(Object mp, Object... p) {
        return null;
    }

    public MapUtil splitValue(String actyDate, String actyDate1, String actyDate2, String s) {
        String[] ary = m.get(actyDate).toString().split(s);

        m.put(actyDate1, ary[0]);

        m.put(actyDate2, ary.length == 1 ? "" : ary[1]);
        return this;
    }

    /***
     *
     * @param m
     * @param rmold
     * @param childMapK if it is not a map, it will skip "put in" to remove itself
     * @return
     */
    public static Map richMap(Map m, boolean rmold, String... childMapK) {
        return getMapPrivate(m, rmold, childMapK);
    }

    private static Map getMapPrivate(Map m, boolean rmold, String[] childMapK) {
        if (childMapK == null) {
            return m;
        }
        for (String chmk : childMapK) {

            richMapAtom(m, rmold, chmk);
        }
        return m;
    }

    private static Map richMapAtom(Map m, boolean rmold, String childMapK) {
        return richMapAtomPrivate(m, rmold, childMapK);
    }

    private static Map richMapAtomPrivate(Map m, boolean rmold, String childMapK) {
        String json = (String) m.get(childMapK);
        Map actybody = JsonToMapUtils.parseJsonToMap(json);

        if (rmold) {
            m.remove(childMapK);
            m.putAll(actybody);
            return m;
        } else {
            m.putAll(actybody);
            return m;
        }
    }
}
